home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / etc_-_Configuration_Files / DHCLIENT.{2D < prev    next >
Text File  |  1999-09-17  |  6KB  |  189 lines

  1. #!/bin/sh
  2. # dhclient-script for Linux. Dan Halbert, March, 1997.
  3. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  4. # No guarantees about this. I'm a novice at the details of Linux
  5. # networking.
  6.  
  7. # Notes:
  8.  
  9. # 0. This script is based on the netbsd script supplied with dhcp-970306.
  10.  
  11. # 1. ifconfig down apparently deletes all relevant routes and flushes
  12. # the arp cache, so this doesn't need to be done explicitly.
  13.  
  14. # 2. The alias address handling here has not been tested AT ALL.
  15. # I'm just going by the doc of modern Linux ip aliasing, which uses
  16. # notations like eth0:0, eth0:1, for each alias.
  17.  
  18. # 3. I have to calculate the network address, and calculate the broadcast
  19. # address if it is not supplied. This might be much more easily done
  20. # by the dhclient C code, and passed on.
  21.  
  22. # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
  23. # of the $1 in its args.
  24.  
  25. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  26. function exit_with_hooks() {
  27.   exit_status=$1
  28.   if [ -x /etc/dhclient-exit-hooks ]; then
  29.     . /etc/dhclient-exit-hooks
  30.   fi
  31. # probably should do something with exit status of the local script
  32.   exit $exit_status
  33. }
  34.  
  35. # Invoke the local dhcp client enter hooks, if they exist.
  36. if [ -x /etc/dhclient-enter-hooks ]; then
  37.   exit_status=0
  38.   . /etc/dhclient-enter-hooks
  39.   # allow the local script to abort processing of this state
  40.   # local script must set exit_status variable to nonzero.
  41.   if [ $exit_status -ne 0 ]; then
  42.     exit $exit_status
  43.   fi
  44. fi
  45.  
  46. release=`uname -r`
  47. release=`expr $release : '\(.*\)\..*'`
  48. relmajor=`echo $release |sed -e 's/^\([^\.]*\)\..*$/\1/'`
  49. relminor=`echo $release |sed -e 's/^.*\.\([^\.]*\)$/\1/'`
  50.  
  51. if [ x$new_broadcast_address != x ]; then
  52.   new_broadcast_arg="broadcast $new_broadcast_address"
  53. fi
  54. if [ x$old_broadcast_address != x ]; then
  55.   old_broadcast_arg="broadcast $old_broadcast_address"
  56. fi
  57. if [ x$new_subnet_mask != x ]; then
  58.   new_subnet_arg="netmask $new_subnet_mask"
  59. fi
  60. if [ x$old_subnet_mask != x ]; then
  61.   old_subnet_arg="netmask $old_subnet_mask"
  62. fi
  63. if [ x$alias_subnet_mask != x ]; then
  64.   alias_subnet_arg="netmask $alias_subnet_mask"
  65. fi
  66.  
  67. if [ x$reason = xMEDIUM ]; then
  68.   # Linux doesn't do mediums (ok, ok, media).
  69.   exit_with_hooks 0
  70. fi
  71.  
  72. if [ x$reason = xPREINIT ]; then
  73.   if [ x$alias_ip_address != x ]; then
  74.     # Bring down alias interface. Its routes will disappear too.
  75.     ifconfig $interface:0- inet 0
  76.   fi
  77.   if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
  78.    then
  79.     ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
  80.         broadcast 255.255.255.255 up
  81.     # Add route to make broadcast work. Do not omit netmask.
  82.     route add default dev $interface netmask 0.0.0.0
  83.   else
  84.     ifconfig $interface up
  85.   fi
  86.  
  87.   # We need to give the kernel some time to get the interface up.
  88.   sleep 1
  89.  
  90.   exit_with_hooks 0
  91. fi
  92.  
  93. if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
  94.   exit_with_hooks 0
  95. fi
  96.   
  97. if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
  98.    [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
  99.   if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
  100.         [ x$alias_ip_address != x$old_ip_address ]; then
  101.     # Possible new alias. Remove old alias.
  102.     ifconfig $interface:0- inet 0
  103.   fi
  104.   if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
  105.     # IP address changed. Bringing down the interface will delete all routes,
  106.     # and clear the ARP cache.
  107.     ifconfig $interface inet down
  108.  
  109.   fi
  110.   if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
  111.      [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
  112.  
  113.     ifconfig $interface inet $new_ip_address $new_subnet_arg \
  114.                             $new_broadcast_arg
  115.     # Add a network route to the computed network address.
  116.     if [ $relmajor -lt 2 ] || \
  117.         ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  118.       route add -net $new_network_number $new_subnet_arg dev $interface
  119.     fi
  120.     for router in $new_routers; do
  121.       route add default gw $router
  122.     done
  123.   fi
  124.   if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
  125.    then
  126.     ifconfig $interface:0- inet 0
  127.     ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  128.     route add -host $alias_ip_address $interface:0
  129.   fi
  130.   echo search $new_domain_name >/etc/resolv.conf
  131.   for nameserver in $new_domain_name_servers; do
  132.     echo nameserver $nameserver >>/etc/resolv.conf
  133.   done
  134.   exit_with_hooks 0
  135. fi
  136.  
  137. if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ]; then
  138.   if [ x$alias_ip_address != x ]; then
  139.     # Turn off alias interface.
  140.     ifconfig $interface:0- inet 0
  141.   fi
  142.   if [ x$old_ip_address != x ]; then
  143.     # Shut down interface, which will delete routes and clear arp cache.
  144.     ifconfig $interface inet down
  145.   fi
  146.   if [ x$alias_ip_address != x ]; then
  147.     ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  148.     route add -host $alias_ip_address $interface:0
  149.   fi
  150.   exit_with_hooks 0
  151. fi
  152.  
  153. if [ x$reason = xTIMEOUT ]; then
  154.   if [ x$alias_ip_address != x ]; then
  155.     ifconfig $interface:0- inet 0
  156.   fi
  157.   ifconfig $interface inet $new_ip_address $new_subnet_arg \
  158.                     $new_broadcast_arg
  159.   set $new_routers
  160.   ############## what is -w in ping?
  161.   if ping -q -c 1 $1; then
  162.     if [ x$new_ip_address != x$alias_ip_address ] && \
  163.             [ x$alias_ip_address != x ]; then
  164.       ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  165.       route add -host $alias_ip_address dev $interface:0
  166.     fi
  167.     if [ $relmajor -lt 2 ] || \
  168.         ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  169.       route add -net $new_network_number
  170.     fi
  171.     for router in $new_routers; do
  172.       route add default gw $router
  173.     done
  174.     echo search $new_domain_name >/etc/resolv.conf.std
  175.     for nameserver in $new_domain_name_servers; do
  176.       echo nameserver $nameserver >>/etc/resolv.conf.std
  177.     done
  178.     if [ -f /etc/resolv.conf ]; then
  179.       rm -f /etc/resolv.conf
  180.       ln /etc/resolv.conf.std /etc/resolv.conf
  181.     fi
  182.     exit_with_hooks 0
  183.   fi
  184.   ifconfig $interface inet down
  185.   exit_with_hooks 1
  186. fi
  187.  
  188. exit_with_hooks 0
  189.